#include "XmlFormattedObjectOutputStream.h"

namespace Entities {

XmlFormattedObjectOutputStream::XmlFormattedObjectOutputStream( ostream & _out, DOM::Document<string> & doc, DOM::Element<string> & root ):
ObjectOutputStream(_out), current(root), document(doc) {}

void XmlFormattedObjectOutputStream::writeObject(const Serializable & obj, string name){
	DOM::Element<string> fils = document.createElement("object");
	current.appendChild(fils);
	if (name!="") fils.setAttribute("class", name);
	XmlFormattedObjectOutputStream xmlOut(out, document, fils);
	obj.save(xmlOut);
}

void XmlFormattedObjectOutputStream::writeValue(string & value){
	DOM::Element<string> fils = document.createElement("value");
	DOM::Text<string> txt = document.createTextNode(value);
	
	current.appendChild(fils);
	fils.appendChild(txt);
}

void XmlFormattedObjectOutputStream::flush(){
	document.normalize();
	out << document;
	out.flush();
}

void XmlFormattedObjectOutputStream::writeSeparator(){
}


XmlFormattedObjectOutputStream XmlFormattedObjectOutputStream::getXmlFormattedObjectOutputStream(string & dest){
	ofstream * fichier = new ofstream( dest.c_str(), ios_base::out );

	DOM::DOMImplementation<string> DomImpl = SimpleDOM::DOMImplementation<string>::getDOMImplementation();
	DOM::Document<string> doc = DomImpl.createDocument("", "DocumentModel", NULL);
	DOM::Element<string> ele = doc.getDocumentElement();

	return XmlFormattedObjectOutputStream(*fichier, doc, ele);
}

}